'use strict';

const Controller = require('egg').Controller;

const essayRule = {
  title: 'string',
  content: 'string',
  introduction: 'string',
  tab_id: 'number',
  publish_user_id: 'number',
};
class EssayController extends Controller {
  async getEssayTabList() {
    const { ctx } = this;
    const { list } = await ctx.service.essay.getEssayTabList();
    return ctx.helper.success({
      ctx,
      data: list,
    });
  }

  async getEssayList() {
    const { ctx } = this;
    const essayId = ctx.params.id;
    const userId = ctx.state.user.id;
    if (essayId) {
      const { list } = await ctx.service.essay.getPublishEssayList(essayId, userId);
      return ctx.helper.success({
        ctx,
        data: list,
      });
    }
  }

  async getEssayInfo() {
    const { ctx } = this;
    const essayId = ctx.params.id;
    const userId = ctx.state.user.id;
    const { res } = await ctx.service.essay.getEssayInfo(essayId, userId);
    return ctx.helper.success({
      ctx,
      data: res[0] ? res[0] : { is_like: 0, is_collect: 0 },
    });
  }

  async getSaveEssayList() {
    const { ctx } = this;
    const userId = ctx.params.id;
    if (userId) {
      const { list } = await ctx.service.essay.getSaveEssayList(userId);
      return ctx.helper.success({
        ctx,
        data: list,
      });
    }
  }

  async getEssayDetail() {
    const { ctx } = this;
    const essayId = ctx.params.id;
    if (essayId) {
      const { list } = await ctx.service.essay.getEssayDetail(essayId);
      return ctx.helper.success({
        ctx,
        data: list[0],
      });
    }
  }

  async getUserEssayInfo() {
    const { ctx } = this;
    const userId = ctx.params.id;
    const { list } = await ctx.service.essay.getUserEssayInfo(userId);
    return ctx.helper.success({
      ctx,
      data: list[0],
    });
  }

  async addEssay() {
    const { ctx } = this;
    const data = ctx.request.body;
    const userId = ctx.state.user.id;
    data.publish_user_id = userId;
    data.publish_time = new Date().getTime();
    ctx.validate(essayRule, data);
    const { id } = await ctx.service.essay.addEssay(data, 1);
    return ctx.helper.success({
      ctx,
      data: { id },
    });
  }

  async addSaveEssay() {
    const { ctx } = this;
    const data = ctx.request.body;
    const userId = ctx.state.user.id;
    data.publish_user_id = userId;
    ctx.validate(essayRule, data);
    const { id } = await ctx.service.essay.addEssay(data, 0);
    return ctx.helper.success({
      ctx,
      data: { id },
    });
  }

  async deleteSaveEssay() {
    const { ctx } = this;
    const essayId = ctx.request.body.id;
    const userId = ctx.state.user.id;
    const { deleteSuccess } = await ctx.service.essay.deleteEssay(essayId, userId);
    return ctx.helper.success({
      ctx,
      msg: deleteSuccess ? '删除成功' : '该文章不在草稿箱或删除失败',
    });
  }

  async likeEssay() {
    const { ctx } = this;
    const essay_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (essay_id && user_id) {
      const { insertSuccess } = await ctx.service.essay.likeEssay(Number(essay_id), Number(user_id));
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '点赞成功' : '点赞失败',
      });
    }
    return ctx.helper.paramErr({ ctx });
  }

  async dislikeEssay() {
    const { ctx } = this;
    const essay_id = ctx.params.id;
    const user_id = ctx.state.user.id;
    if (essay_id && user_id) {
      const { updateSuccess } = await ctx.service.essay.dislikeEssay(essay_id, user_id);
      return ctx.helper.success({
        ctx,
        msg: updateSuccess ? '取消点赞成功' : '取消点赞失败',
      });
    }
  }

  async collectEssay() {
    const { ctx } = this;
    const user_id = ctx.state.user.id;
    const essay_id = ctx.params.id;
    if (user_id && essay_id) {
      const { insertSuccess } = await ctx.service.essay.collectEssay(essay_id, user_id);
      return ctx.helper.success({
        ctx,
        msg: insertSuccess ? '收藏成功' : '收藏失败',
      });
    }
  }

  async notcollectEssay() {
    const { ctx } = this;
    const user_id = ctx.state.user.id;
    const essay_id = ctx.params.id;
    if (user_id && essay_id) {
      const { updateSuccess } = await ctx.service.essay.notcollectEssay(essay_id, user_id);
      return ctx.helper.success({
        ctx,
        msg: updateSuccess ? '取消收藏成功' : '取消收藏失败',
      });
    }
  }
}

module.exports = EssayController;
